package com.hllxd.hism.star.util;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * @author xiyong.lxy
 * @date 2021-12-14 18:18
 */
@Slf4j
public class SyslogManager {
    private static ConcurrentLinkedQueue<Object> logQueue = new ConcurrentLinkedQueue<>();

    private static final Long MAX_SIZE = 1000L;

    public static void product(String traceID, String userID, String service, String path) {
        if(userID==null) {
            userID = "unknown";
        }
        JSONObject data = new JSONObject();
        data.put("traceID", traceID);
        data.put("oa", userID);
        data.put("path", service +path);
        data.put("ts", System.currentTimeMillis());

        if(logQueue.size() > MAX_SIZE)  {
            log.info("log queue is full.");
            logQueue.poll();
        }
        logQueue.add(data);
    }


    public static List<Object> consume() {
        List<Object> list = new ArrayList<>();
        for(int i=0;i<MAX_SIZE; ++i) {
            Object data = logQueue.poll();
            if(data==null) {
                break;
            }
            list.add(data);
        }
        return list;
    }

}
